package com.ruoyi.zhmk.config;

import com.ruoyi.carApi.click.CarApiClick;
import com.ruoyi.carApi.init.ApiInit;
import com.ruoyi.faceApi.click.FaceApiClick;
import com.ruoyi.zhmk.domain.ZhmkCarDevice;
import com.ruoyi.zhmk.domain.ZhmkDoorDevice;
import com.ruoyi.zhmk.mapper.ZhmkCarDeviceMapper;
import com.ruoyi.zhmk.mapper.ZhmkDoorDeviceMapper;
import com.ruoyi.zhmk.service.CarCallBack;
import com.ruoyi.zhmk.service.FaceCallBack;
import com.ruoyi.zhmk.utils.CarApiUtil;
import com.ruoyi.zhmk.utils.FaceApiUtil;
import com.ylsoft.android.facerepeater.net.thrift.server.transaction.DoorControlServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: wwy
 * @Date: 2021/6/22 13:35
 */
@Component
public class Init implements CommandLineRunner {
    @Autowired
    private ZhmkCarDeviceMapper zhmkCarDeviceMapper;
    @Autowired
    private ZhmkDoorDeviceMapper zhmkDoorDeviceMapper;
    public static CarCallBack carCallBack = new CarCallBack();
    public static FaceCallBack faceCallBack = new FaceCallBack();
    @Override
    public void run(String... args) throws Exception {
        //初始化车辆sdk
        System.out.println("=================sdk初始化开始====================");
        ApiInit.init();
        System.out.println("=================sdk初始化结束====================");
        //设置回调函数
        System.out.println("=================设置结果回调函数开始====================");
        CarApiClick carApiClick = CarApiUtil.getCarApiClick();
        carApiClick.setResultCallBack(carCallBack);
        System.out.println("=================设置结果回调函数结束====================");
        //初始化，所有设备
        System.out.println("=================初始化设备开始====================");
        List<ZhmkCarDevice> carDevices = zhmkCarDeviceMapper.selectZhmkCarDeviceList(new ZhmkCarDevice());
        List<String> ipList = new ArrayList<>();
        for (ZhmkCarDevice zhmkCarDevice : carDevices) {
            ipList.add(zhmkCarDevice.getIp());
        }
        String[] ips = new String[ipList.size()];
        System.out.println("=================初始化设备列表"+carDevices.toString()+"====================");
        carApiClick.quickConnect(ipList.toArray(ips));
        System.out.println("=================初始化设备结束====================");

        //初始化人脸机
        //连接设备
        System.out.println("=================人脸机初始化开始====================");
        List<ZhmkDoorDevice> zhmkDoorList = zhmkDoorDeviceMapper.selectZhmkDoorDeviceList(new ZhmkDoorDevice());
        FaceApiClick faceApiClick = FaceApiUtil.getFaceApiClick();
        for (ZhmkDoorDevice zhmkDoor : zhmkDoorList) {
            String doorIp = zhmkDoor.getIp();
            String doorPort = zhmkDoor.getPort();
            faceApiClick.connect(doorIp,Integer.parseInt(doorPort));
        }

        System.out.println("=================人脸机设置回调函数开始====================");
        faceApiClick.setFaceCallBack(faceCallBack);
        System.out.println("=================人脸机设置回调函数结束====================");
        //开启一个子线程进行初始化
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        executorService.execute(()->{
            System.out.println("=================人脸机监听服务初始化开始====================");
            DoorControlServer.startServer();
            System.out.println("=================人脸机监听服务初始化结束====================");
        });

        System.out.println("=================人脸机初始化结束====================");
    }
}
